<?php
	class SpringerlinkApiImplDAO implements RepositoryDAO{
		
		
		/**
		 * Get Document Object by doi
		 *
		 * @param String $doi
		 */
		public function searchByDoi($doi){
			
			
			$request_url = str_replace('[QUERY]', "doi:(".$doi.")", RepositoryConnectionProperty::$_url);
			
			RepositoryConnectionProperty::$_url = $request_url;
			
			
			//Connect to the repository
			$conn = new RepositoryConnection();
			
			//The connection returns a DOM from the URL for the HTML page and
			$html=$conn->getConnection();
			
			
			// Find all <li> in <ul>
			foreach($html->find('ul[id=PrimaryManifest]') as $ul)
			{
				foreach($ul->find('li') as $li)
				{
					foreach($li->find('p.title a') as $p){
						$page=$p->href;
					}
				}
			}
			
			
			$conn->__destruct();
			
			RepositoryConnectionProperty::$_url = "http://springerlink.com".$page;
			
			//Connect to the repository
			$conn = new RepositoryConnection();
				
			//The connection returns a DOM from the URL for the HTML page and
			$html=$conn->getConnection();
			
			$document = new Document();
			$metadata = new Metadata();
			$file = new File();
			$keywords = array();
			
			$document->setMetadata(new Metadata());
			$document->setFile(new File());
			$document->getMetadata()->setKeywords($keywords);
			
			
			$doi = $html->find('span.doi span.value');
			if(!empty($doi))
				$document->getMetadata()->setDoi($doi[0]->plaintext);
			
			$title = $html->find('div.text h1 a');
			if(!empty($title))
				$document->getMetadata()->setTitle($title[0]->plaintext);
			
			
			$journal = $html->find('div.primary a');
			if(!empty($journal))
				$document->getMetadata()->setJournal($journal[0]->plaintext);
			
			$pages = $html->find('span.pagination');
			if(!empty($pages))
				$document->getMetadata()->setPages($pages[0]->plaintext);
			
			//Get the authors
			$authors_array = array();
			foreach($html->find('div.text p.authors') as $d){
				//$doi=$d->find('span');
				foreach($d->children as $c)
					$authors_array[]=$c->plaintext."<br>";
			}
			$authors = implode(', ', $authors_array);
			if(!empty($authors))
				$document->getMetadata()->setAuthors(str_replace("<br>", "", $authors));
			
			$pdf = $html->find('div.heading ul li.pdf a');
			if(!empty($pdf))
				$document->getFile()->setUrl("http://springerlink.com".$pdf[0]->href);
			
			
			return $document;
			
		}
		
		/**
		 * Get Document Object by uid
		 * @param String $uid
		 */
		public function searchByUid($uid){}
		
		public function findDoiByISSN($issn, $title){}
		
		public function searchByTitle($title){}
	}